package Power_Asisst.main;

import java.util.List;




import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class ListViewCheckBox extends ArrayAdapter<mItems>{
	private LayoutInflater inflater;
	public ListViewCheckBox(Context context, List<mItems> objects) {
		super(context,R.layout.simplerow,R.id.rowTextView, objects);
		// TODO Auto-generated constructor stub
		inflater = LayoutInflater.from(context);
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// Planet to display
		mItems planet = (mItems) this.getItem(position);

		// The child views in each row.
		CheckBox checkBox;
		TextView textView;
		if(convertView==null)
		{
			convertView = inflater.inflate(R.layout.simplerow,null);
			textView =(TextView)convertView.findViewById(R.id.rowTextView);
			checkBox = (CheckBox)convertView.findViewById(R.id.CheckBox01);
			convertView.setTag(new SelectViewHolder( checkBox,textView));
			checkBox.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					
					CheckBox cb = (CheckBox)arg0;
					mItems planet = (mItems)cb.getTag();
					planet.setChecked(cb.isChecked());
				}
			});
		
	}
	else
	{
		SelectViewHolder viewHoler = (SelectViewHolder)convertView.getTag();
		checkBox = viewHoler.getCheckBox();
		textView = viewHoler.getTextView();
	}
	checkBox.setTag(planet);
	checkBox.setChecked(planet.isChecked());
	
	textView.setText(planet.getName());
	return convertView;
	

	

}
}

class mItems
{
	private String name ="";
	private boolean checked = false;
	public mItems()
	{
		
	}
	public mItems(String name)
	{
		this.name = name;
	}
	public mItems(String name, boolean checked)
	{
		this.name = name;
		this.checked = checked;
	}
	public String getName()
	{
		return name;
		
	}
	public void setName(String name)
	{
		this.name = name;
	}
	public boolean isChecked()
	{
		return checked;
	}
	public void setChecked(boolean checked)
	{
		this.checked = checked;
	}
	public String toString()
	{
		return name;
	}
	public void toggleChecked()
	{
		checked =! checked;
	}
}
 class SelectViewHolder
{
	private CheckBox checkbox;
	private TextView textView;
	public SelectViewHolder(){};
	public SelectViewHolder(CheckBox ck, TextView tv)
	{
		this.checkbox = ck;
		this.textView = tv;
	}
	public CheckBox getCheckBox()
	{
		return checkbox;
	}
	public void setCheckBox(CheckBox cb)
	{
		this.checkbox = cb;
	}
	public TextView getTextView()
	{
		return textView;
	}
	public void setTextView(TextView tv)
	{
		this.textView = tv;
	}
}
